package com.zy.stock.controller;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zy.stock.domain.Pager;
import com.zy.stock.domain.StockCrawlRecord;
import com.zy.stock.dto.StockCrawlRecordDto;
import com.zy.stock.service.StockCrawlRecordService;

@Controller
public class StockCrawlRecordController {
	
	private Logger logger = LoggerFactory.getLogger(StockCrawlRecordController.class);
			
	@Autowired
	private StockCrawlRecordService stockCrawlRecordService;
	
	@RequestMapping(value = "stockCrawlRecordManage")
	public ModelAndView toBillManager() {
		ModelAndView mav = new ModelAndView("stockCrawlRecordManage");
		mav.addObject("menuid",8);
		return mav;
	}
	
	@ResponseBody
	@RequestMapping(value = "getStockCrawlRecordPage")
	public String getStockCrawlRecordPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		ObjectMapper mapper = new ObjectMapper();
		StringWriter writer = new StringWriter();
		String result = "";
		try {
			Integer draw = Integer.parseInt(request.getParameter("draw"));
			Integer start = Integer.parseInt(request.getParameter("start"));
			Integer length = Integer.parseInt(request.getParameter("length"));
			String stockName = request.getParameter("stockName");
			String stockCode = request.getParameter("stockCode");
			String orderColumn = request.getParameter("order[0][column]");
			String columnName = request.getParameter("columns[" + orderColumn + "][data]");
			String orderDir = request.getParameter("order[0][dir]");
			Pager<StockCrawlRecordDto> pager = new Pager<>();
			StockCrawlRecordDto dto = new StockCrawlRecordDto();
			dto.setName(stockName);
			dto.setCode(stockCode);
			pager.setT(dto);
			pager.setPagenow(start / length + 1);
			pager.setSize(length);
			pager.setOrderCol(columnName);
			pager.setOrder(orderDir);
			Pager<List<StockCrawlRecord>> stations = stockCrawlRecordService.findByCase(pager);
			map.put("data", stations.getT());
			map.put("recordsTotal", stations.getTotal());
			map.put("recordsFiltered", stations.getTotal());
			map.put("draw", draw);
			mapper.writeValue(writer, map);
			result = writer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
